//
//  ViewController.swift
//  demo
//
//  Created by zhangzhaoliang on 15/10/27.
//  Copyright © 2015年 zhangzhaoliang. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBAction func btnTest(sender: AnyObject) {
        
        requestzzl1()
    }
    
    func requestzzl2()
    {
        let session = NSURLSession.sharedSession()
        let request: NSMutableURLRequest = NSMutableURLRequest(URL:  NSURL(string: "http://192.168.179.5/api/values")!)
        
        request.HTTPMethod = "POST"
        let params = ["Name":"jameson2", "Password":"password1"] as Dictionary<String, String>
        request.HTTPBody = try?NSJSONSerialization.dataWithJSONObject(params, options:[])
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")

        let task = session.dataTaskWithRequest(request,  completionHandler: { (data, response, error) -> Void in
            let string = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print(string)
        })
        
        task.resume()
    }
    
    func requestzzl1()
    {
        let session = NSURLSession.sharedSession()
        let request: NSMutableURLRequest = NSMutableURLRequest(URL:  NSURL(string: "http://192.168.179.5/api/values")!)
        request.HTTPMethod = "POST"
        let stringPost="Name=123456&Password=3333"// Key and Value
        let data = stringPost.dataUsingEncoding(NSUTF8StringEncoding)
        request.HTTPBody = data
        let task = session.dataTaskWithRequest(request,  completionHandler: { (data, response, error) -> Void in
            let string = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print(string)
        })
        
        task.resume()
    }

    
    
    func requestzzl()
    {
        let session = NSURLSession.sharedSession()
        let request: NSMutableURLRequest = NSMutableURLRequest(URL:  NSURL(string: "http://192.168.179.5/api/values")!)
        
        request.HTTPMethod = "POST"
        //let stringPost="=123456"// Key and Value
        let stringPost="Name=123456"// Key and Value
        let data = stringPost.dataUsingEncoding(NSUTF8StringEncoding)
        request.HTTPBody = data
        
        let task = session.dataTaskWithRequest(request,  completionHandler: { (data, response, error) -> Void in
            let string = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print(string)
        })
        
        task.resume()
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

